import { pageQueryUser, addUserReq, updateUserReq, queryRole, logicDeleteUserById, logicBatchDeleteUserByIds } from '@/api/admin/common';
import { message, messageType } from '@/utils/message'
const state = {
    userRespData: [],
    userTotal: 0,
    role: [],
};
const mutations = {

    // 分页查询用户
    pageQueryUserListMutations(state, param) {
        console.log('userMutations-->', param.data);
        state.userRespData = param.data;
        state.userTotal = param.total;
    },

    queryRoleMutations(state, param) {
        state.role = param.data;
    }
};
const actions = {
    // 分页查询用户
    async pageQueryUserListActions(context, page) {
        await pageQueryUser(page).then(resp => {
            if (!resp) return;
            console.log('userActions-->', resp.data);
            context.commit('pageQueryUserListMutations', resp.data)
        }, error => {
            console.log(error);
        })
    },
    // 添加用户
    async addUserActions(context, form) {
        await addUserReq(form).then(resp => {
            console.log(resp);
            if (!resp) return;
            message(messageType().success, '操作成功')
        }, error => {
            console.log(error);
        })
    },
    // 修改用户
    async updateUserActions(context, form) {
        await updateUserReq(form).then(resp => {
            if (!resp) return;
            console.log(resp);
            message(messageType().success, '操作成功');
        }, error => {
            console.log(error);
        })
    },
    // 逻辑删除
    async logicDelUserActions(context, id) {
        await logicDeleteUserById(id).then(resp => {
            console.log(resp);
            if (!resp) return;
        }, error => {
            console.log(error);
        })
    },
    // 批量逻辑删除
    async logicBatchDeleteUserByIds(context, ids) {
        await logicBatchDeleteUserByIds(ids).then(resp => {
            console.log(resp);
            if (!resp) return;
        }, error => {
            console.log(error);
        })
    },
    // 查询角色
    async queryRoleActions(context) {
        queryRole().then(resp => {
            console.log(resp);
            if (!resp) return;
            context.commit('queryRoleMutations', resp.data)
        }, error => {
            console.log(error);
        })
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
}